package com.tg.base.concurrent;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * 不可变类型测试
 * 证明：线程安全中不可变级别，只是不可变对象或类型
 * 无论在多线程环境或其他任意环境，其值创建后不可变
 * 但不保证就是线程安全
 */
public class ImmutableTest {

    private static Integer target = 0;

    public static void main(String[] args) throws InterruptedException {
        main2();
    }


    public static void main2() throws InterruptedException {
        for (int i = 1; i <= 10; i++) {
            final int id = i;
            Thread thread = new Thread(() -> {
                final Random random = new Random();
                for (int y = 0; y < 100; y++) {
                    target = target + 1;
                    System.out.println("id:" + id + "=" + target);

                    try {
                        TimeUnit.MILLISECONDS.sleep(((Integer) random.nextInt(300)).longValue());

                    }
                    catch (InterruptedException ex){

                    }
                }
            });
            thread.start();
        }

        TimeUnit.SECONDS.sleep(2);

    }
}
